package com.vca.service.service.Impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.vca.common.model.course.CourseAbout;
import com.vca.common.model.course.CoursePackageRelation;
import com.vca.service.dao.course.CoursePackageRelationDao;
import com.vca.service.service.CoursePackageRelationService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.support.TransactionTemplate;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 套课与课程关系表 服务实现类
 * </p>
 *
 * @author chenBing
 * @since 2022-11-16
 */
@Service
@Slf4j
public class CoursePackageRelationServiceImpl extends ServiceImpl<CoursePackageRelationDao, CoursePackageRelation> implements CoursePackageRelationService {

    @Autowired
    private CoursePackageRelationDao dao;

    @Autowired
    private TransactionTemplate transactionTemplate;


    /**
     * 添加单个套课与多个课程关系
     *
     * @param coursePackageRelations
     * @return
     * @author Li
     * @date 2022/11/18 10:41
     */
    @Override
    public boolean addCoursePackageRelation(List<CoursePackageRelation> coursePackageRelations) {
        return transactionTemplate.execute(e->{
            coursePackageRelations.forEach(g->{
                dao.insert(g);
            });
            return Boolean.TRUE;
        });
    }

    /**
     * 修改单个套课和多个课程的关系
     *
     * @param coursePackageRelations
     * @param coursePackageId
     * @return
     */
    @Override
    public boolean updateCoursePackageRelation(List<CoursePackageRelation> coursePackageRelations, Long coursePackageId) {
        //查询单个套课和多个课程的关系
        LambdaQueryWrapper<CoursePackageRelation> wrapper=new LambdaQueryWrapper();
        wrapper.eq(CoursePackageRelation::getCoursePackageId,coursePackageId);
        List<CoursePackageRelation> coursePackageRelationList = dao.selectList(wrapper);
        List<Long> ids=new ArrayList();
        //根据课程id进行分组
        Map<Long, List<CoursePackageRelation>> listMap = coursePackageRelationList.stream().collect(Collectors.groupingBy(e -> e.getCourseId()));
        //进行遍历 将进行修改的删除
        coursePackageRelations.forEach(e->{
            if (ObjectUtil.isNotEmpty(listMap.get(e.getCourseId()))){
                listMap.remove(e.getCourseId());
            }
        });
        //获得未修改的id
        listMap.forEach((k,v)->{
            ids.add(v.get(0).getId().longValue());
        });
        return transactionTemplate.execute(e->{
            //查询是否存在单个套课和课程关系 不存在添加
            coursePackageRelations.forEach(g->{
                CoursePackageRelation coursePackageRelation = dao.selectOne(new LambdaQueryWrapper<CoursePackageRelation>()
                        .eq(CoursePackageRelation::getCoursePackageId, g.getCoursePackageId())
                        .eq(CoursePackageRelation::getCourseId, g.getCourseId()));
                if (ObjectUtil.isEmpty(coursePackageRelation)){
                    dao.insert(g);
                }
            });
            //将未进行修改的单个套课和课程关系删除
            ids.forEach(i->{
                dao.deleteById(i);
            });
            return Boolean.TRUE;
        });
    }
}
